import datetime

from sqlalchemy.orm import Session

from module_admin.dao.cms_car_dao import CmsCarDao
from module_admin.dao.cms_device_dao import CmsDeviceDao
from module_admin.dao.cms_driver_dao import CmsDriverDao
from module_admin.dao.cms_order_bill_dao import CmsOrderBillDao
from module_admin.dao.cms_order_dao import CmsOrderDao
from module_admin.dao.cms_order_dispatch_dao import CmsOrderDispatchDao
from module_admin.dao.cms_order_execute_dao import CmsOrderExecuteDao
from module_admin.entity.vo.cms_car_vo import CarModel, CarForQuery
from module_admin.entity.vo.cms_device_vo import DeviceForQuery
from module_admin.entity.vo.cms_driver_vo import DriverForQuery
from module_admin.entity.vo.cms_home_vo import HomeStatsSum, MyAgentSum, CarKanban, DriverKanban, MyTime, \
    HomeStatsSumToday, OrderStartAndMileage
from module_admin.entity.vo.cms_order_bill_vo import OrderBillModelForQuery
from module_admin.entity.vo.cms_order_vo import OrderModelForQuery
from module_admin.enum.state_enum import OrderStateEnum


class CmsHomeService:

    @classmethod
    def stats_sum(cls, db: Session):
        # 驾驶员数量
        driver_num = len(CmsDriverDao.list(db, DriverForQuery()))
        car_num = len(CmsCarDao.list(db, CarForQuery()))
        device_num = len(CmsDeviceDao.list(db, DeviceForQuery()))
        home_stats_sum: HomeStatsSum = HomeStatsSum()
        home_stats_sum.driver_num = driver_num
        home_stats_sum.car_num = car_num
        home_stats_sum.device_num = device_num
        return home_stats_sum

    @classmethod
    def my_agent(cls, db: Session):
        query = OrderModelForQuery()
        query.state = OrderStateEnum.dsh.value
        dsh_num = len(CmsOrderDao.list(db, query))
        query.state = OrderStateEnum.ddd.value
        ddd_num = len(CmsOrderDao.list(db, query))
        for_query = OrderBillModelForQuery()
        for_query.state = "未结算"
        js_num = len(CmsOrderBillDao.list(db, for_query))
        sum_num = dsh_num + ddd_num + js_num
        agent_sum = MyAgentSum()
        agent_sum.dsh_num = dsh_num
        agent_sum.ddd_num = ddd_num
        agent_sum.js_num = js_num
        agent_sum.sum_num = sum_num
        return agent_sum

    @classmethod
    def car_kanban(cls, db: Session):
        query = CarForQuery()
        query.state = 0
        not_use_num = len(CmsCarDao.list(db, query))
        query.state = 1
        dao_list = CmsCarDao.list(db, query)
        free_num = 0
        use_num = 0
        for dao in dao_list:
            order_id_list = CmsOrderExecuteDao.get_order_id_list_by_car_id(db, dao.id)
            if len(order_id_list) > 0:
                use_num = use_num + 1
            else:
                free_num = free_num + 1
        kanban = CarKanban()
        kanban.not_use_num = not_use_num
        kanban.free_num = free_num
        kanban.use_num = use_num
        return kanban

    @classmethod
    def driver_kanban(cls, db: Session):
        query = DriverForQuery()
        dao_list = CmsDriverDao.list(db, query)
        free_num = 0
        use_num = 0
        for dao in dao_list:
            order_id_list = CmsOrderExecuteDao.get_order_id_list_by_driver_id(db, dao.id)
            if len(order_id_list) > 0:
                use_num = use_num + 1
            else:
                free_num = free_num + 1
        kanban = DriverKanban()
        kanban.free_num = free_num
        kanban.use_num = use_num
        return kanban

    @classmethod
    def stats_sum_today(cls, db: Session):
        sum_today = HomeStatsSumToday()
        today = MyTime()
        yesterday = MyTime()
        before = MyTime()
        now = datetime.datetime.now()
        today.start_time = now.replace(hour=0, minute=0, second=0, microsecond=0)
        today.end_time = now.replace(hour=23, minute=59, second=59, microsecond=999)
        yesterday.start_time = today.start_time - datetime.timedelta(days=1)
        yesterday.end_time = today.end_time - datetime.timedelta(days=1)
        before.start_time = today.start_time - datetime.timedelta(days=2)
        before.end_time = today.end_time - datetime.timedelta(days=2)
        # 今日里程(任务在今日完成总打表里程)
        sum_today.today_mileage = CmsOrderExecuteDao.get_sum_mileage_by_time(db, today)
        sum_today.yesterday_mileage = CmsOrderExecuteDao.get_sum_mileage_by_time(db, yesterday)
        sum_today.before_mileage = CmsOrderExecuteDao.get_sum_mileage_by_time(db, before)
        # 当日出车(今日司机开始任务数)
        sum_today.today_car_start = CmsOrderExecuteDao.get_sum_order_start_by_time(db, today)
        sum_today.yesterday_car_start = CmsOrderExecuteDao.get_sum_order_start_by_time(db, yesterday)
        sum_today.before_car_start = CmsOrderExecuteDao.get_sum_order_start_by_time(db, before)
        # 空闲车辆(今天没有任务的车辆)
        sum_today.today_car_free = CmsCarDao.get_sum_free_by_time(db, today)
        sum_today.yesterday_car_free = CmsCarDao.get_sum_free_by_time(db, yesterday)
        sum_today.before_car_free = CmsCarDao.get_sum_free_by_time(db, before)
        # 待办任务(order创建时间为今天,状态为这些)
        today_dsh_sum = CmsOrderDao.get_sum_state_by_time(db, today, OrderStateEnum.dsh.value)
        yesterday_dsh_sum = CmsOrderDao.get_sum_state_by_time(db, yesterday, OrderStateEnum.dsh.value)
        before_dsh_sum = CmsOrderDao.get_sum_state_by_time(db, before, OrderStateEnum.dsh.value)
        today_ddd_sum = CmsOrderDao.get_sum_state_by_time(db, today, OrderStateEnum.ddd.value)
        yesterday_ddd_sum = CmsOrderDao.get_sum_state_by_time(db, yesterday, OrderStateEnum.ddd.value)
        before_ddd_sum = CmsOrderDao.get_sum_state_by_time(db, before, OrderStateEnum.ddd.value)
        today_ywj_sum = CmsOrderDao.get_sum_state_by_time(db, today, OrderStateEnum.ywj.value)
        yesterday_ywj_sum = CmsOrderDao.get_sum_state_by_time(db, yesterday, OrderStateEnum.ywj.value)
        before_ywj_sum = CmsOrderDao.get_sum_state_by_time(db, before, OrderStateEnum.ywj.value)
        sum_today.today_order_dsh = today_dsh_sum
        sum_today.today_order_ddd = today_ddd_sum
        sum_today.today_order_ywj = today_ywj_sum
        sum_today.today_order_agent = today_dsh_sum + today_ddd_sum + today_ywj_sum
        sum_today.yesterday_order_dsh = yesterday_dsh_sum
        sum_today.yesterday_order_ddd = yesterday_ddd_sum
        sum_today.yesterday_order_ywj = yesterday_ywj_sum
        sum_today.yesterday_order_agent = yesterday_dsh_sum + yesterday_ddd_sum + yesterday_ywj_sum
        sum_today.before_order_dsh = before_dsh_sum
        sum_today.before_order_ddd = before_ddd_sum
        sum_today.before_order_ywj = before_ywj_sum
        sum_today.before_order_agent = before_dsh_sum + before_ddd_sum + before_ywj_sum
        return sum_today

    @classmethod
    def order_start_and_mileage(cls, db):
        return_list = []
        today = MyTime()
        now = datetime.datetime.now()
        today.start_time = now.replace(hour=0, minute=0, second=0, microsecond=0)
        today.end_time = now.replace(hour=23, minute=59, second=59, microsecond=999)
        my_list = [-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
        for i in my_list:
            day = MyTime()
            day.start_time = today.start_time + datetime.timedelta(days=i)
            day.end_time = today.end_time + datetime.timedelta(days=i)
            order_start_and_mileage = OrderStartAndMileage()
            order_start_and_mileage.driver_num = CmsDriverDao.order_satrt_driver_num(db, day)
            order_start_and_mileage.device_num = CmsDeviceDao.order_satrt_device_num(db, day)
            order_start_and_mileage.car_num = CmsCarDao.order_satrt_car_num(db, day)
            order_start_and_mileage.scale_time = day.start_time
            return_list.append(order_start_and_mileage)
        return return_list
